/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package com.tm.action.feixin.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * How to send a request via proxy using {@link HttpClient}.
 *
 * @since 4.0
 */
public class ClientExecuteProxy {

    public static void main(String[] args)throws Exception {
    	
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
//            HttpGet req = new HttpGet("https://webim.feixin.10086.cn/content/WebIM/SendSMS.aspx");
            HttpPost postMethod = new HttpPost("https://webim.feixin.10086.cn/content/WebIM/SendSMS.aspx");
            
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
	        nvps.add(new BasicNameValuePair("referer", "baiduapp"));
	        nvps.add(new BasicNameValuePair("UserName", "1084298054"));
	        nvps.add(new BasicNameValuePair("Message", "55555"));
	        nvps.add(new BasicNameValuePair("Receivers", "1084298054"));
	        nvps.add(new BasicNameValuePair("ssid", "1084298054p729-992cec27-4f60-4802-8433-293a5035fb67"));
	        postMethod.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
            
            HttpResponse rsp = httpclient.execute(postMethod);
            

            rsp.setHeader("Accept", "application/json, text/javascript, */*");
            rsp.setHeader("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
            rsp.setHeader("Accept-Encoding", "gzip,deflate,sdch");
            rsp.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4");
            rsp.setHeader("Cookie", "IsCookiesEnable=true; __utma=96992031.1318581219.1333462047.1335748670.1335755254.27; webim_sessionid=1084298054p729-992cec27-4f60-4802-8433-293a5035fb6; webim_usersid=571605743; webim_userstatus=400");
            rsp.setHeader("Connection", "keep-alive");
            rsp.setHeader("Cache-Control", "max-age=0");
            rsp.setHeader("Content-Type", "application/x-www-form-urlencoded");
        	rsp.setHeader("Host", "webim.feixin.10086.cn");
        	rsp.setHeader("Origin", "https://webim.feixin.10086.cn");
        	rsp.setHeader("Referer", "https://webim.feixin.10086.cn/content/freeSms.htm?tabIndex=0");
        	rsp.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11");
        	rsp.setHeader("X-Requested-With", "XMLHttpRequest");
            
            HttpEntity entity = rsp.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i<headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }

        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }

}